import random
import time

def print_receipt(shoppingCart, total_value, card_balance):
    print("\n购物小条")
    print("购物时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    print("卡余额:", f"{card_balance:.2f} 元")
    print("购物数量:", sum(item['quantity'] for item in shoppingCart))
    print("购买总商品的原价值:", f"{total_value:.2f} 元")
    print("商品列表:")
    for item in shoppingCart:
        print(f"- {item['product'][0]}: {item['product'][1]:.2f} 元 x {item['quantity']}")

if __name__ == '__main__':
    money = 1000.0
    total_value = 0.0
    print("商品列表")
    shopList = [['小米手机', 500], ['华为手机', 500], ['vivo手机', 500], ['oppo手机', 500], ['三只松鼠', 500], ['百草园', 500],
                ['百事可乐', 100], ['可口可乐', 100], ['北冰洋', 100], ['芬达', 100]]
    for i in range(len(shopList)):
        print(shopList[i])

    print("商品编号")
    for j in range(len(shopList)):
        print(j, shopList[j])

    print("优惠券编号")
    couponsList = [['小米手机', 0.8], ['华为手机', 0.9], ['vivo手机', 0.8], ['oppo手机', 0.7], ['三只松鼠', 0.6], ['百草园', 0.6],
                   ['百事可乐', 0.5], ['可口可乐', 0.5], ['北冰洋', 0.5], ['芬达', 0.5]]
    for j in range(len(couponsList)):
        print(j, couponsList[j])

    shoppingCart = []  # 定义购物车
    print("欢迎光临，请抽取优惠券：")
    b = random.randint(0, 9)
    print("你抽取的优惠券是：", couponsList[b])

    while True:
        print("1. 添加购物车")
        print("2. 退货商品")
        print("3. 退出并打印购物小条")
        choice = input("请选择操作（1/2/3）：").strip()

        if choice == '1':
            num = input("请输入您要购买的商品编号: ").strip()
            if num.isdigit():
                num = int(num)
                if 0 <= num < len(shopList):
                    product = shopList[num]
                    price = product[1]
                    # 计算折扣价格
                    if product[0] == couponsList[b][0]:
                        final_price = price * couponsList[b][1]
                    else:
                        final_price = price

                    # 检查余额是否足够
                    if money >= final_price:
                        # 查找购物车中是否已有该商品
                        for item in shoppingCart:
                            if item['product'][0] == product[0]:
                                item['quantity'] += 1
                                break
                        else:
                            shoppingCart.append({'product': product, 'quantity': 1})

                        money -= final_price
                        total_value += price
                        print(f"恭喜，购买成功！您的卡余额还剩: {money:.2f} 元")
                    else:
                        print("穷鬼，钱不够！请到其他超市购买！")
                else:
                    print("没有这个商品，别瞎弄！")
            else:
                print("请输入正确的商品编号")
        elif choice == '2':
            return_product_name = input("请输入退货商品名称: ").strip()
            for item in shoppingCart:
                if item['product'][0] == return_product_name:
                    if item['quantity'] > 1:
                        item['quantity'] -= 1
                    else:
                        shoppingCart.remove(item)
                    if item['product'][0] == couponsList[b][0]:
                        money += item['product'][1] * couponsList[b][1]
                    else:
                        money += item['product'][1]
                    total_value -= item['product'][1]
                    print(f"退货成功！退还商品: {item['product'][0]}。卡余额：{money:.2f} 元")
                    break
            else:
                print("购物车中没有找到该商品")
        elif choice == '3':
            print_receipt(shoppingCart, total_value, money)
            break
        else:
            print("请输入正确的操作选项")
